package com.colin.library.view.remote;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.SystemClock;
import android.widget.RemoteViews;

import com.colin.library.view.R;
import com.colin.library.view.help.ViewLogHelp;

/**
 * Implementation of App Widget functionality.
 */
public class AppWidget extends AppWidgetProvider {

    private final static String ACTION_SINGLE_CLICK = "com.colin.library.view.remote.SingleClick";

    public AppWidget() {
        super();
    }

    /**
     * 每接收一次广播消息就调用一次，使用频繁
     *
     * @param context
     * @param intent
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        ViewLogHelp.d("action-->>" + intent.getAction());
        //判断自己的action 做自己的事
        if (intent.getAction().equals(ACTION_SINGLE_CLICK)) {
            startImageAnim(context);
            Intent activityIntent = new Intent();
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activityIntent.setClassName(context, "com.colin.app.activity.main.MainActivity");
            context.startActivity(activityIntent);
        }
    }

    /**
     * 小部件被添加时或每次小部件更新时都会调用一次该方法，小部件更新时机是由updatePeriodMillis来指定，每个周期小部件都会自动更新
     *
     * @param context
     * @param appWidgetManager
     * @param appWidgetIds
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            ViewLogHelp.d("appWidgetId-->>" + String.valueOf(appWidgetId));
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    /**
     * 更新桌面部件
     *
     * @param context
     * @param appWidgetManager
     * @param appWidgetId
     */
    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        ViewLogHelp.d("updateAppWidget");
        // Construct the RemoteViews object
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
        remoteViews.setTextViewText(R.id.appwidget_text, context.getString(R.string.appwidget_text));
//        // Instruct the widget manager to update the widget
//        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

        //桌面单击图标 发送Intent广播
        Intent intent_click = new Intent();
        intent_click.setAction(ACTION_SINGLE_CLICK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent_click, 0);
        remoteViews.setOnClickPendingIntent(R.id.image_app_widget_icon, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }

    /**
     * 没删除一次桌面小部件，调用一次
     *
     * @param context
     * @param appWidgetIds
     */
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    /**
     * 当窗口小部件第一次添加到桌面时调用，可添加多次但是只第一次调用
     *
     * @param context
     */
    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    /**
     * 当最后一个给类型的桌面小部件被删除时调用该方法，eg:最后一个
     *
     * @param context
     */
    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }


    /**
     * 单击操作
     *
     * @param context
     */
    private void startImageAnim(final Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap_icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.person);
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                for (int i = 0; i < 37; i++) {
                    float degree = (i * 10) % 360;
                    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
                    remoteViews.setImageViewBitmap(R.id.image_app_widget_icon, rotateBitmap(bitmap_icon, degree));
                    Intent intent_click = new Intent();
                    intent_click.setAction(ACTION_SINGLE_CLICK);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent_click, 0);
                    remoteViews.setOnClickPendingIntent(R.id.image_app_widget_icon, pendingIntent);
                    appWidgetManager.updateAppWidget(new ComponentName(context, AppWidget.class), remoteViews);
                    SystemClock.sleep(30);
                }
            }
        }).start();

    }

    /**
     * 旋转图片
     *
     * @param bitmap_icon
     * @param degree
     * @return
     */
    private Bitmap rotateBitmap(Bitmap bitmap_icon, float degree) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.setRotate(degree);
        return bitmap_icon == null ? null : Bitmap.createBitmap(bitmap_icon, 0, 0, bitmap_icon.getWidth(), bitmap_icon.getHeight());
    }

}

